package com.sunda.spmswms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.feign.IFeignServiceCommon;
import com.sunda.spmswms.service.IDataToSap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @program: spms
 * @description:
 * @author: Wayne Wu
 * @create: 2021-07-05 11:03
 **/
@Service
public class DataToSap implements IDataToSap {

    @Autowired
    IFeignServiceCommon iFeignServiceCommon;

    /** 其他出入库————无源的移仓，提交SAP */
    @Override
    public JSONObject submitDataToSap(JSONObject doc) {
        JSONObject message = JSONObject.parseObject(iFeignServiceCommon.sendRestToSapCommon(doc));
        return message;
    }

    @Override
    public JSONObject formatDataToSap(Map<String, Object> dataToSapHead, List<Map<String, Object>> dataToSapItem, String operation){
        JSONObject doc = new JSONObject();

        JSONObject REQUEST = new JSONObject();
        JSONObject ESB_ATTRS = new JSONObject();
        JSONObject REQUEST_DATA = new JSONObject();

        ESB_ATTRS.put("App_ID", "SPMS");
        ESB_ATTRS.put("Application_ID", "00020000000002");
        ESB_ATTRS.put("Transaction_ID", UUID.randomUUID().toString());

        REQUEST_DATA.put("Items", dataToSapItem);
        REQUEST_DATA.put("Head", dataToSapHead);
        if ("Z_SPMS_ANOMALY_POST".equals(operation)){
            REQUEST_DATA.put("Operation", "Z_SPMS_ANOMALY_POST");
            REQUEST_DATA.put("Type", "ANOMALY_POST");
        }
        if ("Z_SPMS_1150".equals(operation)){
            REQUEST_DATA.put("Operation", "Z_SPMS_1150");
            REQUEST_DATA.put("Type", "SPMS1150");
        }
        if ("Z_SPMS_MIGO".equals(operation)){
            REQUEST_DATA.put("Operation", "Z_SPMS_MIGO");
            REQUEST_DATA.put("Type", "SPMS_MIGO");
        }

        REQUEST.put("ESB_ATTRS", ESB_ATTRS);
        REQUEST.put("REQUEST_DATA", REQUEST_DATA);

        doc.put("REQUEST", REQUEST);
        return doc;
    }

}
